package ru.ifmo.swing.genetics.config.operator;

import org.uncommons.watchmaker.framework.EvaluatedCandidate;
import org.uncommons.watchmaker.framework.EvolutionaryOperator;
import ru.ifmo.automaton.Automaton;
import ru.ifmo.common.Source;
import ru.ifmo.genetics.adaptation.AdaptableParameter;
import ru.ifmo.genetics.mutation.string.RemoveUselessCharsMutation;
import ru.ifmo.swing.EvolutionControlWithConfigProvider;
import ru.ifmo.swing.Panel;
import ru.ifmo.swing.ProgressStartListener;

import javax.swing.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author Roman Kolganov
 *         16.05.12
 */
public class RemoveUselessCharsMutationControl implements
        EvolutionaryOperator<String>,
        EvolutionControlWithConfigProvider,
        AdaptableParameter<String> {
    private RemoveUselessCharsMutation mutation;
    private final CorrectingProbabilityControl probabilityControl;
    private final Panel control;

    public RemoveUselessCharsMutationControl(double defaultMutationProbability,
                                               Source<Automaton<Character, String>> automatonSource) {
        probabilityControl = new CorrectingProbabilityControl(automatonSource, defaultMutationProbability, 0.001);

        control = new Panel();
        control.setHorizontal(false);

        probabilityControl.setDescription("Probability");
        control.add(probabilityControl.getControl());

        mutation = new RemoveUselessCharsMutation(probabilityControl, automatonSource);
    }

    public JComponent getControl() {
        return control;
    }

    public void reset() {
        probabilityControl.reset();
    }

    public void setDescription(String description) {
        control.setTitle(description);
    }

    public List<String> apply(List<String> selectedCandidates, Random rng) {
        return mutation.apply(selectedCandidates, rng);
    }

    public void adapt(List<List<EvaluatedCandidate<String>>> lastGenerations) {
        probabilityControl.adapt(lastGenerations);
    }

    public Map<String, Object> getParamValues() {
        Map<String, Object> res = new HashMap<String, Object>();
        res.put("Probability", probabilityControl);
        return res;
    }
}